<?php
class PageAdmin extends AdminBase
{
	private $nohf = ['pageimgupload','slideimgupload'];
	private $mysql = null;

	public function __construct()
	{

		parent::__construct();
		if(!in_array(N_ACTION,$this->nohf)){
			n_mod('skin')->header('admin');
		}
		$this->mysql = n_api('mysql')->cname('weixin');
	}

	public function index()
	{
		$query = "select * from page_main where isdel ='N'";
		$rs = $this->mysql->select($query);
		$this->_html('index', $rs);
	}

	public function pageEdit()
	{
		$param = $this->param('pageId:char:empty');
		$pageId = $param['pageId'];
		$data['pageInfo'] = n_api('page')->getPageInfoById($pageId);
		$this->_html('pageEdit', $data);
	}

	public function pageImgUpload()
	{
		if ($_FILES['upload_file']['error'] != UPLOAD_ERR_OK) {
			switch($_FILES['upload_file']['error']) {
				//其值为 1，上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值
				case UPLOAD_ERR_INI_SIZE:
				$msg = 'The upload file exceeds the upload_max_filesize directive in php.ini';
				break;

				//其值为 2，上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值
				case UPLOAD_ERR_FORM_SIZE:
				$msg = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.';
				break;

				//其值为 3，文件只有部分被上传
				case UPLOAD_ERR_PARTIAL:
				$msg = 'The uploaded file was only partially uploaded.';
				break;

				//其值为 4，没有文件被上传
				case UPLOAD_ERR_NO_FILE:
				$msg = 'No file was uploaded.';
				break;

				//其值为 6，找不到临时文件夹
				case UPLOAD_ERR_NO_TMP_DIR:
				$msg = 'The server is missing a temporary folder.';
				break;

				//其值为 7，文件写入失败
				case UPLOAD_ERR_CANT_WRITE:
				$msg = 'The server failed to write the uploaded file to disk.';
				break;

				//其他异常
				case UPLOAD_ERR_EXTENSION:
				$msg = 'File upload stopped by extension.';
				break;
    		}
    		echo json_encode(['status'=>false, 'info'=>$msg]);
    		return;
		}

		$type = $_FILES['upload_file']['type'];
		$time = time();
		$baseDir = N_PLUGINS . '/page/img/';

		switch ($type) {
			case 'image/gif':
				if ($image = imagecreatefromgif($_FILES['upload_file']['tmp_name'])) {
					$imageName = md5($pageId.'fuckfina'.$time).'.gif';
					$imgPath = $baseDir . $imageName;
					imagegif($image,$imgPath);
				}
				else
					$msg = '不支持的文件格式！';
			break;

			case 'image/jpeg':
				if ($image = imagecreatefromjpeg($_FILES['upload_file']['tmp_name'])) {
					$imageName = md5($pageId.'fuckfina'.$time).'.jpg';
					$imgPath = $baseDir . $imageName;
					imagejpeg($image,$imgPath);
				}
				else
					$msg = '不支持的文件格式！';
			break;

			case 'image/png':
				if ($image = imagecreatefrompng($_FILES['upload_file']['tmp_name'])) {
					$imageName = md5($pageId.'fuckfina'.$time).'.png';
					$imgPath = $baseDir . $imageName;
					imagepng($image,$imgPath);
				}
				else
					$msg = '不支持的文件格式！';
			break;

			default:
				$msg = '不支持的文件格式！';
		}

		if (isset($msg))
			echo json_encode(['status'=>false, 'info'=>$msg]);
		else
			echo json_encode(['status'=>true, 'info'=>$imageName]);
	}

	public function pageUpdate()
	{
		$param = $this->param('
			pageId:char:empty,
			imgName:char:ignore,
			pageDetail:char:empty,
			title:char
		');

		$time = time();
		if (isset($param['imgName']))
			$query = "update page_main set img_name = '{$param['imgName']}', detail = '{$param['pageDetail']}', utime = $time where id = {$param['pageId']}";
		else
			$query = "update page_main set title='{$param['title']}' detail = '{$param['pageDetail']}', utime = $time where id = {$param['pageId']}";
		$this->mysql->exec($query);

		echo "ok";
	}

	public function show()
	{
		$param = $this->param('pageId:char:empty');
		$pageId = $param['pageId'];

		$query = "select * from page_main where id = $pageId";
		$rs = $this->mysql->fetchFirst($query);

		$this->_html('show', $rs);
	}

	public function slide()
	{
		$rs = $this->mysql->select("select * from page_slide where isdel ='N'");
		$this->_html('slide', $rs);
	}

	public function slideEdit()
	{
		$param = $this->param('sid:char:empty');
		$sid = $param['sid'];

		$data['sid'] = $sid;
		$data['slideInfo'] = $this->mysql->fetchFirst("select * from page_slide where id = $sid and isdel = 'N'");
		$this->_html('slideEdit', $data);
	}

	public function slideImgUpload()
	{
		if ($_FILES['upload_file']['error'] != UPLOAD_ERR_OK) {
			switch($_FILES['upload_file']['error']) {
				//其值为 1，上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值
				case UPLOAD_ERR_INI_SIZE:
				$msg = 'The upload file exceeds the upload_max_filesize directive in php.ini';
				break;

				//其值为 2，上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值
				case UPLOAD_ERR_FORM_SIZE:
				$msg = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.';
				break;

				//其值为 3，文件只有部分被上传
				case UPLOAD_ERR_PARTIAL:
				$msg = 'The uploaded file was only partially uploaded.';
				break;

				//其值为 4，没有文件被上传
				case UPLOAD_ERR_NO_FILE:
				$msg = 'No file was uploaded.';
				break;

				//其值为 6，找不到临时文件夹
				case UPLOAD_ERR_NO_TMP_DIR:
				$msg = 'The server is missing a temporary folder.';
				break;

				//其值为 7，文件写入失败
				case UPLOAD_ERR_CANT_WRITE:
				$msg = 'The server failed to write the uploaded file to disk.';
				break;

				//其他异常
				case UPLOAD_ERR_EXTENSION:
				$msg = 'File upload stopped by extension.';
				break;
    		}
    		echo json_encode(['status'=>false, 'info'=>$msg]);
    		return;
		}

		$type = $_FILES['upload_file']['type'];
		$time = time();
		$baseDir = N_PLUGINS . '/page/slide/';

		switch ($type) {
			case 'image/gif':
				if ($image = imagecreatefromgif($_FILES['upload_file']['tmp_name'])) {
					$imageName = md5($pageId.'fuckfina'.$time).'.gif';
					$imgPath = $baseDir . $imageName;
					imagegif($image,$imgPath);
				}
				else
					$msg = '不支持的文件格式！';
			break;

			case 'image/jpeg':
				if ($image = imagecreatefromjpeg($_FILES['upload_file']['tmp_name'])) {
					$imageName = md5($pageId.'fuckfina'.$time).'.jpg';
					$imgPath = $baseDir . $imageName;
					imagejpeg($image,$imgPath);
				}
				else
					$msg = '不支持的文件格式！';
			break;

			case 'image/png':
				if ($image = imagecreatefrompng($_FILES['upload_file']['tmp_name'])) {
					$imageName = md5($pageId.'fuckfina'.$time).'.png';
					$imgPath = $baseDir . $imageName;
					imagepng($image,$imgPath);
				}
				else
					$msg = '不支持的文件格式！';
			break;

			default:
				$msg = '不支持的文件格式！';
		}

		if (isset($msg))
			echo json_encode(['status'=>false, 'info'=>$msg]);
		else
			echo json_encode(['status'=>true, 'info'=>$imageName]);
	}

	public function slideUpdate()
	{

		$param = $this->param('
			title:char:empty,
			sid:char:empty,
			imgName:char:ignore,
			pageDetail:char:empty
		');

		$time = time();
		if (isset($param['imgName']))
			$query = "update page_slide set name = '{$param['title']}', img_name = '{$param['imgName']}', detail = '{$param['pageDetail']}', utime = $time where id = {$param['sid']}";
		else
			$query = "update page_slide set name = '{$param['title']}', detail = '{$param['pageDetail']}', utime = $time where id = {$param['sid']}";

		$this->mysql->exec($query);
		n_mod("skin")->save("保存成功",3,'/page/slide.html');
	}

	public function __destruct(){
		//加入标准尾。
		if(!in_array(N_ACTION,$this->nohf) && 'page'==N_TYPE){
			n_mod('skin')->footer();
		}
	}


}